/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import reseauSocial.ReseauSocial;
import sysRecmd.SysRecmd;
import core.Content;
import core.FileUtils;
import core.Review;
import core.SetBeer;
import core.SetUser;

/**
 *
 * @author Le
 */
public class SYRRES {
	public static final int NB_NOTE = 5;
	public static final int SQUARE_LOSS = 0;
	public static final int HINGE_LOSS = 1;	

    private static ArrayList<Review> allReview = new ArrayList<Review>();
    private static ArrayList<Content> allContent = new ArrayList<Content>();
    
    private static HashMap<String, String> review_user = new HashMap<String, String>();
    private static HashMap<String, String> review_beer = new HashMap<String, String>();
    private static HashMap<String, List<Integer>> review_note = new HashMap<String, List<Integer>>();
    
    private static SetUser allUser = new SetUser();
	private static SetBeer allBeer = new SetBeer();
	
	private static double[] averageNote = new double[NB_NOTE];
	
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
    	// Charger les donnees
        FileUtils.loadContent(allContent);
        FileUtils.loadRelation(review_user, review_beer);
        FileUtils.loadReviewNote(review_note);
        FileUtils.loadReview(review_note, allReview, review_user, review_beer);
        
        allUser = generateUsers(allReview);
        allBeer = generateBeers(allReview);
        
        for (int i = 0; i < NB_NOTE; i++) {
        	allUser.calculSumNote(i);
        	allBeer.calculSumNote(i);
        	averageNote[i] = (double)allUser.getSumNote(i) / allReview.size();
        }

        // Partie 1 : Systeme de recommandation
        SysRecmd sysRecmd = new SysRecmd(allReview);
//        sysRecmd.trainPerceptron();
        boolean withRegularisation = false;
        boolean withBiais = false;
        sysRecmd.doMatrixFactorization(allBeer, allUser, withRegularisation, withBiais);
        
        // Partie 2 : Reseau Social

//       ReseauSocial reseauSocial = new ReseauSocial(allReview, allUser, allBeer); 
//       int threshold = 5;	// Le threshold de similarite x% pour le graphe non pondere
//       reseauSocial.calculSimilarity(threshold, false);
//       reseauSocial.predictNotes(threshold);
    }   
    
    public static SetUser generateUsers(ArrayList<Review> ensReview) {
    	SetUser ensUser = new SetUser();
    	int userPos;
    	for (Review rev : allReview) {
    		userPos = ensUser.hasUser(rev.getUser());
			if (userPos == -1) {
				ensUser.addUser(rev.getUser());
			}
    	}
    	
    	for (Review rev : ensReview) {
			userPos = ensUser.hasUser(rev.getUser());
			ensUser.getUser(userPos).addBiereNote(rev.getBeer(), rev.getNote());
    	}
    	return ensUser;
    }	
	
	public static SetBeer generateBeers(ArrayList<Review> ensReview) {
		SetBeer ensBeer = new SetBeer();
		int beerPos;
		for (Review rev : allReview) {
			beerPos = ensBeer.hasBeer(rev.getBeer());
			if (beerPos == -1) {
				ensBeer.addBeer(rev.getBeer());
			}
		}
			
		for (Review rev : ensReview) {
			beerPos = ensBeer.hasBeer(rev.getBeer());
			ensBeer.getBeer(beerPos).addUser(rev.getUser(), rev.getNote());
		}
		return ensBeer;
	}
	
	public static double getAverageNote(int noteID) {
		return averageNote[noteID];
	}
	
	public static void addAverageNote(int noteID, double value) {
		averageNote[noteID] += value;
	}
	    
    public static int getContentSize() {
        return allContent.size();
    }  
    
    public static String getContent(int i) {
        return allContent.get(i).getContent();
    }
    
    public static int getUserSize() {
    	return allUser.size();
    }
    
    public static int getBeerSize() {
    	return allBeer.size();
    }
}
